package com.votesys.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;

public class UtilsImpl implements IUtils{
	
	/**
	 * change file type to byte array type
	 * @param file
	 * @return
	 * @throws IOException 
	 */
	public byte[] getFileInputStream(File tempfile) throws IOException{
		FileInputStream fis = null;
		int len = 0;
		byte[] buffer = new byte[1024];
		byte[] tempoutput = null;
		ByteArrayOutputStream fos = null;
		if(tempfile!=null){
			fis = new FileInputStream(tempfile);
			fos = new ByteArrayOutputStream();
			while((len=fis.read(buffer))!=-1){
				fos.write(buffer,0,len);
			}
			tempoutput = fos.toByteArray();
		}else{
			System.out.println("file is empty");
		}
		return tempoutput;
	}
	
	public InputStream getFileInputStream(byte[] file){
	    InputStream input = new ByteArrayInputStream(file);
	    return input;
	}
	public HttpServletRequest getRequest(){
		ActionContext ac = ActionContext.getContext();
		HttpServletRequest request = (HttpServletRequest) ac.get(ServletActionContext.HTTP_REQUEST);
		return request;
	}

}
